package com.aricwang.easyprofile.rule;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.aricwang.easyprofile.contentprovider.RuleContentProvider;
import com.aricwang.easyprofile.database.RuleTable;

public class Rule {
	public static final int SUN = 0x00000001;
	public static final int MON = 0x00000002;
	public static final int TUE = 0x00000004;  
	public static final int WED = 0x00000008;  
	public static final int THU = 0x00000010;  
	public static final int FRI = 0x00000020;  
	public static final int SAT = 0x00000040;  
	
	public static final int WEEK = (SUN|MON|TUE|WED|THU|FRI|SAT);
	public static final int WEEKDAYS = (MON|TUE|WED|THU|FRI);

	public static final int DONT_CARE = 0;
	public static final int CHARGING = 1;
	public static final int NOT_CHARGING = 2;
	
	public static final int NO_PLACE = -1;
	
	public static final int PRIORITY_STEP = 10;

	// rule 
	public int mRuleId;
	public String mRuleName;
	public int mPriority;
	
	// conditions
	public int mStartHour;
	public int mStartMinute;
	public int mEndHour;
	public int mEndMinute;
	public int mRepeat;
	
	public int mCharging;
	
	public int mPlaceId;
	
	// profile to apply
	public int mProfileId;
	
	/////////////////////////////////////////////////////////////////////////////////////////
	// Static functions
	/////////////////////////////////////////////////////////////////////////////////////////

	// get Rule from database
	static Rule getRule(Context context, int id) {
		Rule r = null;
		
		Uri uri = Uri.parse(RuleContentProvider.CONTENT_URI + "/" + id);

		Cursor c = null;
		try {
			c = context.getContentResolver().query(uri, RuleTable.ALL_COLUMNS, null, null, null); 
		} catch (Exception e){
			throw  new IllegalArgumentException("ERROR");
		};
		
		if (c == null)
			return r;
		
		if (c.getCount() > 0) {
			// must move to first, otherwise, get exception
			c.moveToFirst();
			
			r = new Rule();
			
			// get the item from database
			r.mRuleId = c.getInt(c.getColumnIndex(RuleTable.COLUMN_ID));
			r.mRuleName = c.getString(c.getColumnIndex(RuleTable.COLUMN_NAME));
			r.mPriority = c.getInt(c.getColumnIndex(RuleTable.COLUMN_PRIORITY));
			r.mStartHour = c.getInt(c.getColumnIndex(RuleTable.COLUMN_TIMEFRAME_START_HOUR));
			r.mStartMinute = c.getInt(c.getColumnIndex(RuleTable.COLUMN_TIMEFRAME_START_MINUTE));
			r.mEndHour = c.getInt(c.getColumnIndex(RuleTable.COLUMN_TIMEFRAME_END_HOUR));
			r.mEndMinute = c.getInt(c.getColumnIndex(RuleTable.COLUMN_TIMEFRAME_END_MINUTE));
			r.mRepeat = c.getInt(c.getColumnIndex(RuleTable.COLUMN_TIMEFRAME_REPEAT));
			r.mCharging = c.getInt(c.getColumnIndex(RuleTable.COLUMN_CHARGING));
			r.mPlaceId = c.getInt(c.getColumnIndex(RuleTable.COLUMN_PLACE));
			r.mProfileId = c.getInt(c.getColumnIndex(RuleTable.COLUMN_PROFILE));
			
		}
	
		c.close();
		
		return r;
	}

	
	static int deleteRule(Context context, int id) {
		Uri uri = Uri.parse(RuleContentProvider.CONTENT_URI + "/" + id);
		return context.getContentResolver().delete(uri, null, null);
	}
	
	static Uri insertRule(Context context, Rule rule) {
		ContentValues values = ruleToContentValues(rule);
		return context.getContentResolver().insert(RuleContentProvider.CONTENT_URI, values);
	}
	
	static int updateRule(Context context, Rule rule) {
		Uri uri = Uri.parse(RuleContentProvider.CONTENT_URI + "/" + rule.mRuleId);
		ContentValues values = ruleToContentValues(rule);
		return context.getContentResolver().update(uri, values, null, null);
	}
	
	static private ContentValues ruleToContentValues(Rule rule) {
		ContentValues values = new ContentValues();
		values.put(RuleTable.COLUMN_NAME, rule.mRuleName);
		values.put(RuleTable.COLUMN_PRIORITY, rule.mPriority);
		values.put(RuleTable.COLUMN_PROFILE, rule.mProfileId);
		values.put(RuleTable.COLUMN_TIMEFRAME_START_HOUR, rule.mStartHour);
		values.put(RuleTable.COLUMN_TIMEFRAME_START_MINUTE, rule.mStartMinute);
		values.put(RuleTable.COLUMN_TIMEFRAME_END_HOUR, rule.mEndHour);
		values.put(RuleTable.COLUMN_TIMEFRAME_END_MINUTE, rule.mEndMinute);
		values.put(RuleTable.COLUMN_TIMEFRAME_REPEAT, rule.mRepeat);
		values.put(RuleTable.COLUMN_CHARGING, rule.mCharging);
		values.put(RuleTable.COLUMN_PLACE, rule.mPlaceId);

		return values;
	}
	
	static public int getHighestPriority(Context context) {
		int priority = 0;
		
    	Cursor cursor = context.getContentResolver().query(RuleContentProvider.CONTENT_URI, 
				new String[] { RuleTable.COLUMN_ID, RuleTable.COLUMN_PRIORITY}, 
				null, null, RuleTable.COLUMN_PRIORITY + " desc");
		if (cursor != null) {
			if (cursor.getCount() > 0) {
				cursor.moveToFirst();
				priority = cursor.getInt(cursor.getColumnIndex(RuleTable.COLUMN_PRIORITY));
			}
			cursor.close();
		}
		
		return priority;
	}
	

	static public int getNewRulePriority(Context context) {
		return (Rule.PRIORITY_STEP + getHighestPriority(context));
	}

}
